package org.giovanolli.mumi.export;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;

import org.giovanolli.mumi.entities.Patient;
import org.giovanolli.mumi.entities.PatientEvaluation;
import org.giovanolli.mumi.util.DateUtils;
import org.giovanolli.mumi.view.util.FacesContextUtils;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Table;

/**
 * The Class PatientEvaluationTemplate.
 */
public class PatientEvaluationTemplate extends AbstractTemplate<PatientEvaluation> {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.giovanolli.mumi.export.AbstractTemplate#applyContent(com.lowagie.text.Document, org.giovanolli.mumi.entities.IdentificableEntity)
	 */
	@Override
	protected void applyContent(Document document, PatientEvaluation patientEvaluation) throws BadElementException, MalformedURLException, DocumentException, IOException {
		document.add(generateHeader(patientEvaluation.getFromDate(), patientEvaluation.getToDate(), patientEvaluation.getId()));
		document.add(generatePatientData(patientEvaluation.getPatient()));
		document.add(generateEvaluationBody(patientEvaluation));
		document.add(generateFooter());
	}

	private final Table generateEvaluationBody(PatientEvaluation patientEvaluation) throws BadElementException {
		Table evaluationBody = new Table(1);
		evaluationBody.setWidth(100);
		evaluationBody.setPadding(2f);

		Cell evaluationCell = new Cell();
		evaluationCell.setVerticalAlignment("Center");
		evaluationCell.setHorizontalAlignment("Left");

		evaluationCell.add(new Chunk(patientEvaluation.getEvaluation()));
		evaluationBody.addCell(evaluationCell);

		return evaluationBody;
	}

	private final Table generateFooter() throws BadElementException {
		Table patientEvaluationFooter = new Table(1);
		patientEvaluationFooter.setWidth(100);
		patientEvaluationFooter.setPadding(2f);
		patientEvaluationFooter.setBottom(400);
		Cell content = generateTableHeaderCell("DOCUMENTO NO VALIDO COMO FACTURA");
		patientEvaluationFooter.addCell(content);
		return patientEvaluationFooter;
	}

	/**
	 * Generate header.
	 * 
	 * @return the table
	 * @throws BadElementException
	 *             the bad element exception
	 * @throws MalformedURLException
	 *             the malformed url exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	private final Table generateHeader(Date fromDate, Date toDate, Integer id) throws BadElementException, MalformedURLException, IOException {
		Table header = new Table(2);
		header.setWidth(100);
		header.setPadding(2f);

		// Logo cell
		Cell logoCell = getFormattedForHeaderCell();
		String logoPath = FacesContextUtils.getRealPath() + File.separator + "resources" + File.separator + "images" + File.separator + "logo_90_45.png";
		logoCell.add(Image.getInstance(logoPath));
		logoCell.add(new Chunk("Centro de Atencion Integral"));
		header.addCell(logoCell);

		// Title cell
		Cell titleCell = getFormattedForHeaderCell();
		titleCell.add(new Chunk("EVALUACION DE PACIENTE", FONT_TABLE_HEADER));
		titleCell.add(Chunk.NEWLINE);
		titleCell.add(this.generateKeyValueChunk("Numero", String.format("%08d", id)));
		titleCell.add(Chunk.NEWLINE);
		titleCell.add(this.generateKeyValueChunk("Desde", DateUtils.getSimpleFormattedDate(fromDate)));
		titleCell.add(Chunk.NEWLINE);
		titleCell.add(this.generateKeyValueChunk("Hasta", DateUtils.getSimpleFormattedDate(toDate)));
		header.addCell(titleCell);
		return header;
	}

	/**
	 * Generate patient data.
	 * 
	 * @return the table
	 * @throws BadElementException
	 *             the bad element exception
	 */
	private final Table generatePatientData(Patient patient) throws BadElementException {
		Table patientData = new Table(1);
		patientData.setWidth(100);
		patientData.setPadding(2f);

		// name cell
		Cell nameCell = new Cell();
		nameCell.setVerticalAlignment("Center");
		nameCell.setHorizontalAlignment("Left");
		nameCell.add(generateKeyValueChunk("Nombre", patient.getFullName()));
		nameCell.add(Chunk.NEWLINE);
		nameCell.add(generateKeyValueChunk("Documento No.", patient.getDocumentNumber()));
		patientData.addCell(nameCell);

		return patientData;
	}
}
